﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class SecondMemBase
    {
        public long Add(Chain.Model.SecondMemBase model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SecondMemBase(");
            builder.Append("SMB_MemID,SMB_Create,SMB_UserID,SMB_PageControl,SMB_PageControlType,SMB_PageControlValues,SMB_Status)");
            builder.Append(" values (");
            builder.Append("@SMB_MemID,@SMB_Create,@SMB_UserID,@SMB_PageControl,@SMB_PageControlType,@SMB_PageControlValues,@SMB_Status)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SMB_MemID", SqlDbType.Int, 4), new SqlParameter("@SMB_Create", SqlDbType.DateTime), new SqlParameter("@SMB_UserID", SqlDbType.Int, 4), new SqlParameter("@SMB_PageControl", SqlDbType.VarChar, 30), new SqlParameter("@SMB_PageControlType", SqlDbType.TinyInt, 1), new SqlParameter("@SMB_PageControlValues", SqlDbType.NVarChar, 0x400), new SqlParameter("@SMB_Status", SqlDbType.Bit, 1) };
            cmdParms[0].Value = model.SMB_MemID;
            cmdParms[1].Value = model.SMB_Create;
            cmdParms[2].Value = model.SMB_UserID;
            cmdParms[3].Value = model.SMB_PageControl;
            cmdParms[4].Value = model.SMB_PageControlType;
            cmdParms[5].Value = model.SMB_PageControlValues;
            cmdParms[6].Value = model.SMB_Status;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0L;
            }
            return Convert.ToInt64(single);
        }

        public bool Delete(long SMB_ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SecondMemBase ");
            builder.Append(" where SMB_ID=@SMB_ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SMB_ID", SqlDbType.BigInt) };
            cmdParms[0].Value = SMB_ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string SMB_IDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SecondMemBase ");
            builder.Append(" where SMB_ID in (" + SMB_IDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(long SMB_ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SecondMemBase");
            builder.Append(" where SMB_ID=@SMB_ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SMB_ID", SqlDbType.BigInt) };
            cmdParms[0].Value = SMB_ID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataTable GetList(int MemID)
        {
            return this.GetList(string.Format("SMB_Status=1 AND SMB_MemID='{0}'", MemID)).Tables[0];
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SMB_ID,SMB_MemID,SMB_Create,SMB_UserID,SMB_PageControl,SMB_PageControlType,SMB_PageControlValues,SMB_Status ");
            builder.Append(" FROM SecondMemBase ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" SMB_ID,SMB_MemID,SMB_Create,SMB_UserID,SMB_PageControl,SMB_PageControlType,SMB_PageControlValues,SMB_Status ");
            builder.Append(" FROM SecondMemBase ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.SMB_ID desc");
            }
            builder.Append(")AS Row, T.*  from SecondMemBase T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.SecondMemBase GetModel(long SMB_ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 SMB_ID,SMB_MemID,SMB_Create,SMB_UserID,SMB_PageControl,SMB_PageControlType,SMB_PageControlValues,SMB_Status from SecondMemBase ");
            builder.Append(" where SMB_ID=@SMB_ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SMB_ID", SqlDbType.BigInt) };
            cmdParms[0].Value = SMB_ID;
            Chain.Model.SecondMemBase base2 = new Chain.Model.SecondMemBase();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["SMB_ID"] != null) && (set.Tables[0].Rows[0]["SMB_ID"].ToString() != ""))
            {
                base2.SMB_ID = long.Parse(set.Tables[0].Rows[0]["SMB_ID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SMB_MemID"] != null) && (set.Tables[0].Rows[0]["SMB_MemID"].ToString() != ""))
            {
                base2.SMB_MemID = int.Parse(set.Tables[0].Rows[0]["SMB_MemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SMB_Create"] != null) && (set.Tables[0].Rows[0]["SMB_Create"].ToString() != ""))
            {
                base2.SMB_Create = DateTime.Parse(set.Tables[0].Rows[0]["SMB_Create"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SMB_UserID"] != null) && (set.Tables[0].Rows[0]["SMB_UserID"].ToString() != ""))
            {
                base2.SMB_UserID = int.Parse(set.Tables[0].Rows[0]["SMB_UserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SMB_PageControl"] != null) && (set.Tables[0].Rows[0]["SMB_PageControl"].ToString() != ""))
            {
                base2.SMB_PageControl = set.Tables[0].Rows[0]["SMB_PageControl"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SMB_PageControlType"] != null) && (set.Tables[0].Rows[0]["SMB_PageControlType"].ToString() != ""))
            {
                base2.SMB_PageControlType = int.Parse(set.Tables[0].Rows[0]["SMB_PageControlType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SMB_PageControlValues"] != null) && (set.Tables[0].Rows[0]["SMB_PageControlValues"].ToString() != ""))
            {
                base2.SMB_PageControlValues = set.Tables[0].Rows[0]["SMB_PageControlValues"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SMB_Status"] != null) && (set.Tables[0].Rows[0]["SMB_Status"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["SMB_Status"].ToString() == "1") || (set.Tables[0].Rows[0]["SMB_Status"].ToString().ToLower() == "true"))
                {
                    base2.SMB_Status = true;
                    return base2;
                }
                base2.SMB_Status = false;
            }
            return base2;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SecondMemBase ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool SetMemBase(List<Chain.Model.SecondMemBase> listmodel)
        {
            ArrayList sQLStringList = new ArrayList();
            sQLStringList.Add(string.Format("IF(EXISTS(SELECT 1 FROM dbo.SecondMemBase WHERE SMB_MemID='{0}' AND SMB_Status=1))BEGIN UPDATE dbo.SecondMemBase SET SMB_Status=0 WHERE SMB_Status=1 AND SMB_MemID='{0}';END", listmodel[0].SMB_MemID));
            for (int i = 0; i < listmodel.Count; i++)
            {
                sQLStringList.Add(listmodel[i].getInsrtSqlCommand());
            }
            return DbHelperSQL.ExecuteSqlTran(sQLStringList);
        }

        public bool SetStatus(int MemID)
        {
            return (DbHelperSQL.ExecuteSql(string.Format("UPDATE dbo.SecondMemBase SET SMB_Status=0 WHERE SMB_Status=1 AND SMB_MemID='{0}'", MemID)) > 0);
        }

        public bool Update(Chain.Model.SecondMemBase model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SecondMemBase set ");
            builder.Append("SMB_MemID=@SMB_MemID,");
            builder.Append("SMB_Create=@SMB_Create,");
            builder.Append("SMB_UserID=@SMB_UserID,");
            builder.Append("SMB_PageControl=@SMB_PageControl,");
            builder.Append("SMB_PageControlType=@SMB_PageControlType,");
            builder.Append("SMB_PageControlValues=@SMB_PageControlValues,");
            builder.Append("SMB_Status=@SMB_Status");
            builder.Append(" where SMB_ID=@SMB_ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SMB_MemID", SqlDbType.Int, 4), new SqlParameter("@SMB_Create", SqlDbType.DateTime), new SqlParameter("@SMB_UserID", SqlDbType.Int, 4), new SqlParameter("@SMB_PageControl", SqlDbType.VarChar, 30), new SqlParameter("@SMB_PageControlType", SqlDbType.TinyInt, 1), new SqlParameter("@SMB_PageControlValues", SqlDbType.NVarChar, 0x400), new SqlParameter("@SMB_Status", SqlDbType.Bit, 1), new SqlParameter("@SMB_ID", SqlDbType.BigInt, 8) };
            cmdParms[0].Value = model.SMB_MemID;
            cmdParms[1].Value = model.SMB_Create;
            cmdParms[2].Value = model.SMB_UserID;
            cmdParms[3].Value = model.SMB_PageControl;
            cmdParms[4].Value = model.SMB_PageControlType;
            cmdParms[5].Value = model.SMB_PageControlValues;
            cmdParms[6].Value = model.SMB_Status;
            cmdParms[7].Value = model.SMB_ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

